<?php

namespace App\Http\Controllers\Content;

use App\Http\Controllers\Controller;
use App\Models\ContentTemplateModel;
use App\User;
use Illuminate\Http\Request;

class TemplateController extends Controller
{

    public function index()
    {
//        dd(User::firstOrNew(['id'=>88]));
//        dd(\DB::table('users')->select('id')->where('id','like','1_')->get());
        //模型中并没有数据,数据都存放在数据库表中,我们只是可以通过模型方便从数据库中取数据
//        dd(array_random(\DB::table('users')->select('name')->get()->all(),3));
//        dd(json_decode(view('admin.layouts.ceshi').'',true));
//        dd( json_decode(file_get_contents(glob(public_path('theme').'/*')[0].'/package.json'),true));
//        dd(\DB::table('users')->get());
//        dd( gettype(view('admin.layouts.ceshi').''));
//        通过上面的配置我们就能获取到渲染的视图中的html中的内容
        $themes = ContentTemplateModel::get();
        //渲染视图
        return view('content.template_index',compact('themes'));
    }

    //方法名括号中使用类型约束
    public function cache(ContentTemplateModel $contentTemplate){
        //获取最新模板列表
        $contentTemplate->loadTemplate();
        return back()->with('success','重新加载模板成功');
    }


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\ContentTemplateModel  $contentTemplate
     * @return \Illuminate\Http\Response
     */
    public function show(ContentTemplateModel $contentTemplate)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\ContentTemplateModel  $contentTemplate
     * @return \Illuminate\Http\Response
     */
    public function edit(ContentTemplateModel $contentTemplate)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\ContentTemplateModel  $contentTemplate
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, ContentTemplateModel $template)
    {
        //之所以这里要再次调用loadTemplate()方法,就是要先刷新模板列表,保证一次只有一个模板处于选中状态
          $template->loadTemplate();
          $template['using'] = TRUE;
          $template->save();
          return back()->with('success','模板修改成功');


    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\ContentTemplateModel  $contentTemplate
     * @return \Illuminate\Http\Response
     */
    public function destroy(ContentTemplateModel $contentTemplate)
    {
        //
    }
}
